package pers.cz.testNg;

import com.jayway.jsonpath.JsonPath;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: PostGril_1.0
 * @description: PostGirlParser
 * @author: Cheng Zhi
 * @create: 2021-11-07 17:09
 **/
public class PostGirlParser {

    // 1、定义所有的数学符号
    //private final String tokents[] = {"≠", "≤", "≥", "⊆", "⊇", "=", ">", "<"};
    private final String NEQ = "≠";
    private final String LESS_EQUALS = "≤";
    private final String GREAT_EQUALS = "≥";
    private final String RIGHT_CONTAIN = "⊆";
    private final String LEFT_CONTAIN = "⊇";
    private final String EQ = "=";
    private final String GREAT = ">";
    private final String LESS = "<";

    private static final Map<String,String> tokensMap;

    static {
        tokensMap = new HashMap();
        tokensMap.put("NEQ","≠");
        tokensMap.put("LESS_EQUALS","≤");
        tokensMap.put("GREAT_EQUALS","≥");
        tokensMap.put("RIGHT_CONTAIN","⊆");
        tokensMap.put("LEFT_CONTAIN","⊇");
        tokensMap.put("EQ","=");
        tokensMap.put("GREAT",">");
        tokensMap.put("LESS","<");
    }


    private final Map<String,Integer> map = new HashMap<>();



    // 2、判断传入的表达式中是否包含已定义的数学公式
    // 词法分析
    // 种别码
    /*
    <±,1>
    <≠,2>
    <≤,3>
    <≥,4>
    <⊆,5>
    <⊇,6>
    <=,7>
    <>,8>
    <<,9>
     */

    /**
     * 解析器
     * @param input
     */
    public static Boolean blockLexer(String jsonStr, String input) {

        // 1、预处理表达式 $[*].so_nbr ⊇ 2,
        Boolean isPass = false;
        for (Map.Entry<String,String> token : tokensMap.entrySet()) {

            if (input.indexOf(token.getValue()) != -1) {
                isPass = processor(jsonStr, input, token.getKey());
            }
        }

        return isPass;
    }

    /**
     * 解析jsonPath
     * @param jsonStr
     * @param expression
     * @param token
     * @return
     */
    private static Boolean processor(String jsonStr, String expression, String token) {

        Boolean isPass = false;
        String[] split = expression.split(tokensMap.get(token)); // 返回了表达式
        Object read = JsonPath.read(jsonStr, split[0].trim());
        String rightStr = split[1].trim();
        switch (token) {
            case "NEQ" :
                isPass = dealNEQ(read, rightStr);
                break;
            case "LESS_EQUALS" :
                isPass = dealLESS_EQUALS(read, rightStr);
                break;
            case "GREAT_EQUALS" :
                isPass = dealGREAT_EQUALS(read, rightStr);
                break;
            case "RIGHT_CONTAIN" :
                isPass = dealRIGHT_CONTAIN(read, rightStr);
                break;
            case "LEFT_CONTAIN" :
                isPass = dealLEFT_CONTAIN(read, rightStr);
                break;
            case "EQ" :
                isPass = dealEQ(read, rightStr);
                break;
            case "GREAT" :
                isPass = dealGREAT(read, rightStr);
                break;
            case "LESS" :
                isPass = dealLESS(read, rightStr);
                break;
        }

        return isPass;
    }

    /**
     * 处理不等于
     * @param read
     * @param str
     * @return
     */
    private static Boolean dealNEQ(Object read, String str) {

        return read.toString().equals(str);
    }

    /**
     * 处理小于等于
     * @param read
     * @param str
     * @return
     */
    private static Boolean dealLESS_EQUALS(Object read, String str) {

        return Integer.valueOf(read.toString()) <= Integer.valueOf(str);
    }

    /**
     * 处理大于等于
     * @param read
     * @param str
     * @return
     */
    private static Boolean dealGREAT_EQUALS(Object read, String str) {
        return Integer.valueOf(read.toString()) >= Integer.valueOf(str);
    }

    /**
     * 处理右包含
     * @param read
     * @param str
     * @return
     */
    private static Boolean dealRIGHT_CONTAIN(Object read, String str) {
        return str.contains(read.toString().trim());
    }

    /**
     * 处理左包含
     * @param read
     * @param str
     * @return
     */
    private static Boolean dealLEFT_CONTAIN(Object read, String str) {
        return read.toString().contains(str);
    }

    /**
     * 处理相等
     * @param read
     * @param str
     * @return
     */
    private static Boolean dealEQ(Object read, String str) {
        return read.toString().equals(str);
    }

    /**
     * 处理大于
     * @param read
     * @param str
     * @return
     */
    private static Boolean dealGREAT(Object read, String str) {
        return Integer.valueOf(read.toString()) > Integer.valueOf(str);
    }

    /**
     * 处理小于
     * @param read
     * @param str
     * @return
     */
    private static Boolean dealLESS(Object read, String str) {
        return Integer.valueOf(str) > Integer.valueOf(read.toString());
    }

    /**
     * @param data 指定字符串
     * @param str 需要定位的特殊字符或者字符串
     * @param num   第n次出现
     * @return  第n次出现的位置索引
     */
    public static int getIndexOf(String data,String str,int num){
        Pattern pattern = Pattern.compile(str);
        Matcher findMatcher = pattern.matcher(data);
        //标记遍历字符串的位置
        int indexNum=0;
        while(findMatcher.find()) {
            indexNum++;
            if(indexNum==num){
                break;
            }
        }
        // System.out.println("字符或者字符串"+str+"第"+num+"次出现的位置为："+findMatcher.start());
        return findMatcher.start();
    }


    public static void main(String[] args) {

        String expression = "$[*].so_nbr ⊇ 2";
/*        String

        for (int i=0; i<tokents.length; i++) {

            if (expression.indexOf(tokents[i]) != -1) {
                System.out.println(tokents[i]);
            }
        }*/



    }


}
